/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package genesisserver;

import Client.Client;
import PacketHandling.PacketHandler;

import GameManager.Universe;

import java.util.ArrayList;

/**
 *
 * @author J_Laptop
 */
public class GenesisServer {

    public static boolean exitRequested = false;
    public static int frameRate = 100;
    public static int fps = 0;
    static long packetWait = 100;
    static long packetTime = 0;
    boolean fpsShown = true;
    static PacketHandler packetHandler;
    ArrayList<Client> clientList = new ArrayList<Client>();
    static Universe universe;

    public static void main(String[] args) {
        startServer();
    }

    public static void startServer() {
        System.out.println("Server Started...");
        initNet();
        initGame();

        Thread gameLoop = new Thread() {

            @Override
            public void run() {

                long fpsTime = System.currentTimeMillis();
                long lastTime = System.currentTimeMillis();
                long lastUpdate = System.currentTimeMillis();
                int frames = 0;

                while (!exitRequested) {



                    if (System.currentTimeMillis() - lastTime >= (1000 / frameRate)) {

                        update(System.currentTimeMillis() - lastUpdate);

                        lastUpdate = System.currentTimeMillis();
                        frames++;
                        lastTime = System.currentTimeMillis();

                    }
                    if (System.currentTimeMillis() - fpsTime >= 1000) {


                        fps = frames;
                        frames = 0;
                        fpsTime = System.currentTimeMillis();

                    }

                }

            }
        };

        gameLoop.start();

    }

    public static void initNet() {

        packetHandler = new PacketHandler();

    }

    public static void initGame() {

        try {
            universe = new Universe();
        } catch (NullPointerException e) {

            e.printStackTrace();

        }
    }

    public static void update(long elapsedTime) {

        if (packetTime > packetWait) {

            packetHandler.sendQueuedPackets();
            packetTime = 0;
            
        } else {
            
            packetTime += elapsedTime;

        }


    }
}
